package Utilities;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

// This class handles the creation and writing of log file for a specific module
public class logFileIO
{
	// globals 
	private BufferedWriter logWriter;
	public String logFileName; 
	private String logFileDir;
	
	public logFileIO( String logFileBaseName, String extension )
	{
		try
		{
			// Get the date and do the file/filename creation of the directory and actual filename
			Date date = new java.util.Date();
			logFileDir = "log\\";
			logFileName = new String(logFileDir + logFileBaseName + " " + new Timestamp(date.getTime()) + "." + extension );
			logFileName = logFileName.replaceAll(":", "-");
			File logDir = new File(logFileDir);
			File logFile = new File(logFileName);
			
			// Make the directory and file
			logDir.mkdir();
			if(!logFile.exists())
			{
				logFile.createNewFile();
			}
			
			// Create the BufferedWriter
			FileWriter fileWrite = new FileWriter(logFile.getAbsoluteFile());
			logWriter =  new BufferedWriter(fileWrite);
		}
		catch (IOException e)
		{
			// Uh oh, print the stack before we perish
			e.printStackTrace();
		}
	}
	
	public void write( String sLineToWrite )
	{
		try
		{
			// Write line and newline
			logWriter.write(sLineToWrite);
			logWriter.newLine();
			logWriter.flush();
			
			System.out.println(sLineToWrite);
		}
		catch (IOException e)
		{
			// Uh oh, print the stack before we perish
			e.printStackTrace();
		}
	}
	
	public void close()
	{
		try
		{
			logWriter.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
